#include "Log.hpp"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "Common.hpp"
#include "InetArr.hpp"
#include <functional>
#include "Log.hpp"

using namespace LogModule;
int main(int argc, char *argv[])
{
    // ::signal(SIGINT,handle);

    // ./udp_server 127.0.0.1 8080

    std::string ip = "47.98.139.142";
    uint16_t port = 8080;
    if (argc == 3)
    {
        ip = argv[1];
        port = ::atoi(argv[2]);
    }
    else if (argc > 3 || argc == 2)
    {
        std::cout << "Usage ./client_server 47.98.139.142 8080" << std::endl;
        return 1;
    }

    LOG_CONSOLE();
    // socket connect
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd == -1)
    {
        LOG(LogLevel::ERROR) << "socket failed";
    }
    LOG(LogLevel::DEBUG) << "socket success";

    InetAddr inet(port, ip);

    int n = connect(sockfd, inet.NetAddr(), inet.NetAddrLen());
    if (n == -1)
    {
        LOG(LogLevel::ERROR) << "connect failed";
        DIE(CONNECT_ERROR);
    }
    LOG(LogLevel::DEBUG) << "connect success";


    while (true)
    {
        std::cout << "Please Enter#";
        std::string line;
        getline(std::cin, line);

        ssize_t n = ::send(sockfd, line.c_str(), line.size(), 0);
        if (n == -1)
        {
            LOG(LogLevel::ERROR) << "send failed";
            exit(1);
        }

        char buffer[1024];
        n = ::recv(sockfd, buffer, sizeof(buffer)-1, 0);
        if (n == -1)
        {
            LOG(LogLevel::ERROR) << "send failed";
            exit(1);
        }
        buffer[n]=0;
        LOG(LogLevel::DEBUG) << buffer;
    }

    return 0;
}